﻿using Daemon.Common;
using Daemon.Data.Substructure.Enums;
using Daemon.Repository.EF;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace DaemonAPI.Controllers.V1
{
	[ApiController]
	[Route("api/sessionitems")]
	public class SessionItemV2Controller : BaseApiController
	{
		private readonly SessionItemRepository _sessionItemRepository;

		public SessionItemV2Controller(SessionItemRepository sessionItemRepository)
		{
			_sessionItemRepository = sessionItemRepository;
		}

		[Route("")]
		[HttpPut]
		public ResultModel<SessionItem> Put([FromBody] SessionItem sessionItem)
		{
			sessionItem = string.IsNullOrEmpty(sessionItem.Id) ? _sessionItemRepository.Register(sessionItem, sessionItem.Prefix) : _sessionItemRepository.Update(sessionItem, sessionItem.Prefix);
			return new ResultModel<SessionItem>(sessionItem);
		}

		/// <summary>
		/// Log off
		/// </summary>
		/// <param name="id"></param>
		/// <returns></returns>
		[Route("{id}")]
		[HttpDelete]
		public ResultModel<bool> LogOff(string id)
		{
			return new ResultModel<bool>(_sessionItemRepository.Delete(id, LogoutReasonEnum.UserInitiatedLogout));
		}
	}
}
